<?php
//error_reporting(E_ALL);
set_time_limit(0);
include_once dirname(dirname(dirname(dirname(dirname(__FILE__)))))."/common.php";
\ns_core\m_load::load("ns_model.message.message_explain");
\ns_core\m_load::load("ns_core.m_pinyin");
class message extends message_explain
{
    private $arr_picture = null;

    private $arr_map = null;

	public function explain($message)
	{
	    \ns_core\m_load::load_np("np_xml2array.class.php");
	    $content = $this->get_message_content($message);

	    if($content['ret'] !=0 || strlen($content['data_info']) <1)
	    {
	        return $content;
	    }
	    $str_content = m_config::trim_xml_header($content['data_info']);

	    if(!m_config::is_xml($str_content))
	    {
	        return m_config::return_data(1,"消息GUID[{$message['nns_id']}]xml内容非xml结构");
	    }
	    //解析下载的XML
	    $dom = new DOMDocument('1.0', 'utf-8');

	    $dom->loadXML($str_content);
	    $xml = $dom->saveXML();
	    $xml_arr = np_xml2array::getArrayData($xml);

	    if ($xml_arr["ret"] == 0)
	    {
	        return m_config::return_data(1,"消息GUID[{$message['nns_id']}]xml解析失败");
	    }
        if(!is_array($xml_arr['data']) || empty($xml_arr['data']))
        {
           return m_config::return_data(1,"消息GUID[{$message['nns_id']}]xml解析成功后不是数组");;
        }

	    $Objects[] = $this->mu_array_change_key_case($xml_arr['data']);

	    //Series数据数组
	    $arr_series = array ();
	    //Program数据数组
	    $arr_program = array ();
	    //Movie数据数组
	    $arr_movie = array ();
	    //Picture数据数组
	    $arr_picture = array ();
	    //Channel数据数组
	    $arr_channel = array ();

	    //直播分集数据数组
	    $arr_live_index = array ();

        //直播片源数据数组
	    $arr_live_media = array ();

	    //删除片源数组
	    $arr_del_media = array();

	    //schedulerecord节目单数据数组
	    $arr_playbill = array ();

	    if (!empty($Objects) && is_array($Objects))
	    {
	        foreach ($Objects as $obj)
	        {
	            if ($obj['name'] == 'TransferContent')
	            {
                    $arr=$this->make_key_value_arr_v2($obj);
                    if(isset($arr['capturestart']) && strlen($arr['capturestart']) >0 && isset($arr['captureend']) && strlen($arr['captureend']) >0 )
                    {
                        $arr_playbill[] = $arr;
                        continue;
                    }
                    if(isset($arr['sourceurl']) && strlen(trim($arr['sourceurl'])) >0)
                    {
                        $arr['key_val_list']['input'][0]['sourceurl'] = trim($arr['sourceurl']);
                    }
                    if (!isset($arr['key_val_list']['input']) || !is_array($arr['key_val_list']['input']))
                    {
                        continue;
                    }
                    foreach ($arr['key_val_list']['input'] as $key=>$val)
                    {

                        if (strlen($val['sourceurl']) < 1)
                        {
                            continue;
                        }
                        $transferBitRate = (isset($val['transferbitrate']) && (int)$val['transferbitrate']>0) ? (int)$val['transferbitrate'] : 809 . $key;
                        $arr['media_import_id'] = $arr['assetid'];

                        $arr_parse_url = parse_url($val['sourceurl']);
                        $str_scheme = isset($arr_parse_url['scheme']) ? trim($arr_parse_url['scheme']) : '';
                        $str_scheme = strlen($str_scheme) <1 ? 'http' : $str_scheme;
                        $str_path = isset($arr_parse_url['path']) ? trim($arr_parse_url['path']) : '';
                        $arr_path = strlen($str_path) > 0 ? pathinfo($str_path) : null;
                        $str_extension = isset($arr_path['extension']) ? trim($arr_path['extension']) : '';
                        $str_extension = strlen($str_extension) <1 ? 'ts' : $str_extension;
                        $arr['input'] = $val;
                        if (isset($arr['capturestart']) && !empty($arr['capturestart']))
                        {
                            $arr_live_media[] = $arr;
                            continue;
                        }
                        else if(strtolower($str_scheme) == "udp" || ($str_extension == 'm3u8' && strtolower($str_scheme) == 'http'))
                        {

                            $arr_live_media[] = $arr;
                            continue;
                        }
                        $arr_movie[] = $arr;
                    }

	            }
	            else if ($obj['name'] == 'CancelTransfer')
	            {
	                $arr_del_media[]=$this->make_key_value_arr_v2($obj);
	            }
	            else if ($obj['name'] == 'DeleteContent')
	            {
                    $arr_del_media[] = $this->make_key_value_arr_v2($obj);
	            }
                else if ($obj['name'] == 'DeleteContent')
                {
                    $arr_del_media[] = $this->make_key_value_arr_v2($obj);
                }
                else if ($obj['name'] == 'StartChannel')
                {
                    $arr = $this->make_key_value_arr_v2($obj);

                    if(isset($arr['sourceurl']) && strlen(trim($arr['sourceurl'])) >0)
                    {
                        $arr['key_val_list']['input'][0]['sourceurl'] = trim($arr['sourceurl']);
                    }
                    if (!isset($arr['key_val_list']['input']) || !is_array($arr['key_val_list']['input']))
                    {
                        continue;
                    }
                    foreach ($arr['key_val_list']['input'] as $key=>$val)
                    {
                        if (strlen($val['sourceurl']) < 1)
                        {
                            continue;
                        }
                        $transferBitRate = isset($val['transferbitrate']) ? $val['transferbitrate'] : 808 . $key;
                        $arr['media_import_id'] = $arr['assetid'];
                        $arr['input'] = $val;
                        $arr_live_media[] = $arr;
                    }
                }
                else if ($obj['name'] == 'StopChannel')
                {
                    $arr_del_media[] = $this->make_key_value_arr_v2($obj);
                }
	        }
	    }

	    //主媒资操作   主媒资增删改走以前老逻辑，基本没什么变化
	    if (!empty($arr_series))
	    {
	        foreach ($arr_series as $val_series)
	        {
	            //主媒资删除
	            if (strtoupper($val_series['Action']) == 'DELETE')
	            {
	                $del_series = array(
	                    'base_info'=>array(
	                        'nns_asset_import_id'=>$val_series['ContentID'],
                            'nns_import_source'=>$val_series['nns_import_source'],
                            'nns_cp_id'=>$message['nns_cp_id'],
                        ),
	                );
	                $result = $this->vod_action('delete', $del_series);
	            }
	            else
	            {
	                //默认一级分类为电影
	                $view_type = 0;
	                $asset_category = (isset($val_series['key_val_list']['CategoryName']) && strlen($val_series['key_val_list']['CategoryName']) >0) ? $val_series['key_val_list']['CategoryName'] : '电影';
	                $do_category = array(
	                             'base_info'=>array(
	                                      'nns_name'=>$asset_category,   //栏目名称支持多层目录，多层目录的时候 /分割
	                                      'nns_import_category_id'=>'', //栏目名称支持多层目录，多层目录的时候 /分割
	                                      'nns_cp_id'=>$message['nns_cp_id'],
	                                      'nns_import_parent_category_id'=>'',
	                                      'nns_video_type'=>'0',  //媒资类型  0 点播 | 1 直播
	                              ),
                     );
	                $result = $this->category_action('add', $do_category);
	                if($result['ret'] !=0)
	                {
	                    return $result;
	                }
	                if(!isset($result['data_info']['base_info']['nns_id']) || strlen($result['data_info']['base_info']['nns_id']) <1)
	                {
	                    return m_config::return_data(1,'创建栏目失败');
	                }
	                $nns_category_id = $result['data_info']['base_info']['nns_id'];
	                foreach ($this->category_map as $index => $v)
	                {
	                    if (stripos($v, $asset_category) !== false)
	                    {
	                        $view_type = $index;
	                        break;
	                    }
	                }
	                if($view_type == 0 && isset($val_series['key_val_list']['VolumnCount']) && $val_series['key_val_list']['VolumnCount'] >1)
	                {
	                    $view_type = 1;
	                }
	                #TODO
	                $arr_img = $this->handle_picture_v3($val_series['ContentID']);
	                if (!empty($arr_img) && is_array($arr_img))
	                {
	                    $val_series['key_val_list'] = array_merge($val_series['key_val_list'], $arr_img);
	                }
	                $pinyin = m_pinyin::get_pinyin_letter($val_series['key_val_list']['Name']);
	                $add_series = array(
                        'base_info' => array(
                            'nns_name' => isset($val_series['key_val_list']['Name']) ? $val_series['key_val_list']['Name'] : '',
                            'nns_view_type' => $view_type,
                            'nns_org_type' => '0',
                            'nns_tag' => '26,',
                            'nns_director' => isset($val_series['key_val_list']['WriterDisplay']) ? $val_series['key_val_list']['WriterDisplay'] : '',
                            'nns_actor' => isset($val_series['key_val_list']['ActorDisplay']) ? $val_series['key_val_list']['ActorDisplay'] : '',
                            'nns_show_time' => (isset($val_series['key_val_list']['ReleaseYear']) && strlen($val_series['key_val_list']['ReleaseYear']) > 0) ? date("Y", strtotime($val_series['key_val_list']['ReleaseYear'])) : date("Y"),
                            'nns_view_len' => isset($val_series['key_val_list']['Duration']) ? $val_series['key_val_list']['Duration'] : 0,
                            'nns_all_index' => isset($val_series['key_val_list']['VolumnCount']) ? $val_series['key_val_list']['VolumnCount'] : 1,
                            'nns_new_index' => isset($val_series['key_val_list']['NewCount']) ? $val_series['key_val_list']['NewCount'] : 0,
                            'nns_area' => isset($val_series['key_val_list']['OriginalCountry']) ? $val_series['key_val_list']['OriginalCountry'] : '',
                            'nns_image0' => isset($val_series['key_val_list']['bigpic']) ? $val_series['key_val_list']['bigpic'] : '',
                            'nns_image1' => isset($val_series['key_val_list']['middlepic']) ? $val_series['key_val_list']['middlepic'] : '',
                            'nns_image2' => isset($val_series['key_val_list']['smallpic']) ? $val_series['key_val_list']['smallpic'] : '',
                            'nns_image3' => '',
                            'nns_image4' => '',
                            'nns_image5' => '',
                            'nns_summary' => isset($val_series['key_val_list']['Description']) ? $val_series['key_val_list']['Description'] : '',
                            'nns_remark' => isset($val_series['key_val_list']['Kind']) ? $val_series['key_val_list']['Kind'] : '',
                            'nns_category_id' => $nns_category_id,
                            'nns_play_count' => '0',
                            'nns_score_total' => '0',
                            'nns_score_count' => '0',
                            'nns_point' => isset($val_series['key_val_list']['ViewPoint']) ? $val_series['key_val_list']['ViewPoint'] : '0',
                            'nns_copyright_date' => (isset($val_series['key_val_list']['ReleaseYear']) && strlen($val_series['key_val_list']['ReleaseYear']) > 0) ? date("Y", strtotime($val_series['key_val_list']['ReleaseYear'])) : date("Y"),
                            'nns_asset_import_id' => $val_series['ContentID'],
                            'nns_pinyin' => (isset($val_series['key_val_list']['Spell']) && strlen($val_series['key_val_list']['Spell']) > 0) ? $val_series['key_val_list']['Spell'] : $pinyin,
                            'nns_pinyin_length' => (isset($val_series['key_val_list']['Spell']) && strlen($val_series['key_val_list']['Spell']) > 0) ? strlen($val_series['key_val_list']['Spell']) : strlen($pinyin),
                            'nns_alias_name' => isset($val_series['key_val_list']['AliasName']) ? $val_series['key_val_list']['AliasName'] : '',
                            'nns_eng_name' => isset($val_series['key_val_list']['EnglishName']) ? $val_series['key_val_list']['EnglishName'] : '',
                            'nns_language' => isset($val_series['key_val_list']['Language']) ? $val_series['key_val_list']['Language'] : '',
                            'nns_text_lang' => isset($val_series['key_val_list']['Language']) ? $val_series['key_val_list']['Language'] : '',
                            'nns_producer' => isset($val_series['key_val_list']['ContentProvider']) ? $val_series['key_val_list']['ContentProvider'] : '',
                            'nns_screenwriter' => isset($val_series['key_val_list']['WriterDisplay']) ? $val_series['key_val_list']['WriterDisplay'] : '',
                            'nns_play_role' => '',
                            'nns_copyright_range' => isset($val_series['key_val_list']['ContentProvider']) ? $val_series['key_val_list']['ContentProvider'] : '',
                            'nns_vod_part' => '',
                            'nns_keyword' => isset($val_series['key_val_list']['Keywords']) ? $val_series['key_val_list']['Keywords'] : '',
                            'nns_import_source' => isset($val_series['nns_import_source']) ? $val_series['nns_import_source'] : evn::get("project"),
                            'nns_kind' => isset($val_series['key_val_list']['Kind']) ? $val_series['key_val_list']['Kind'] : '',
                            'nns_copyright' => isset($val_series['key_val_list']['ContentProvider']) ? $val_series['key_val_list']['ContentProvider'] : '',
                            'nns_clarity' => '',
                            'nns_image_v' => isset($val_series['key_val_list']['verticality_img']) ? $val_series['key_val_list']['verticality_img'] : '',
                            'nns_image_s' => isset($val_series['key_val_list']['square_img']) ? $val_series['key_val_list']['square_img'] : '',
                            'nns_image_h' => isset($val_series['key_val_list']['horizontal_img']) ? $val_series['key_val_list']['horizontal_img'] : '',
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_conf_info' => '',
                            'nns_ext_url' => '',
                        ), //基本信息（存储于nns_vod表中）
                        'ex_info' => array(
                            'svc_item_id' => '',
                            'month_clicks' => '',
                            'week_clicks' => '',
                            'base_id' => '',
                            'asset_path' => '',
                            'ex_tag' => '',
                            'full_spell' => '',
                            'awards' => '',
                            'year' => '',
                            'play_time' => '',
                            'channel' => '',
                            'first_spell' => '',
                        ), //扩展信息（存储于nns_vod_ex表中）
                    );
	                //字段待修改4-14
	                $result = $this->vod_action('add', $add_series);
	            }
                if($result['ret'] !=0)
                {
                    break;
                }
	        }
	    }
        //分集操作   分集删除走以前的老逻辑，分集注册  则组装分集和片源的信息到redis，分集更新则查询redis关联数据
        if (!empty($arr_program))
        {
            foreach ($arr_program as $val_program)
            {
                //分集删除
                if (strtoupper($val_program['Action']) == 'DELETE')
                {
                    $del_program = array(
                        'base_info' => array(
                            'nns_asset_import_id' => $val_program['ContentID'],
                            'nns_import_source' => $val_program['nns_import_source'],
                            'nns_cp_id' => $message['nns_cp_id'],
                        ),
                    );
                    $re = $this->index_action('delete', $del_program);
                    if ($re['ret'] != 0)
                    {
                        $re['ret'] = 1;
                        return $re;
                    }
                }
                else
                {
                    if ($message['nns_cp_id'] == 'JSCN' && $val_program['key_val_list']['SeriesFlag'] == 0)
                    {
                        $p_series[0] = $val_program['ContentID'];
                    }
                    else
                    {
                        $arr_p_series = $this->get_map_info_v2('Program', $this->arr_map, $val_program['ContentID']);
                        $p_series = array_keys($arr_p_series);
                        //分集不存在集数字段
                        if (!isset($val_program['key_val_list']['Sequence']) || !strlen($val_program['key_val_list']['Sequence']) > 0)
                        {
                            $val_program['key_val_list']['Sequence'] = isset($arr_p_series[$p_series[0]]['key_val_list']['Sequence']) ? $arr_p_series[$p_series[0]]['key_val_list']['Sequence'] : 0;
                        }
                        if (substr($this->cp_id, 0, 5) == 'fuse_')
                        {
                            $val_program['key_val_list']['Sequence']++;
                        }
                    }
                    $res = nl_asset_incidence_relation::query_asset_relation($this->import->dc, 'video', $p_series[0], $message['nns_cp_id'], 0);
                    if (!is_array($res['data_info']))
                    {
                        return $re['ret'] = 6;
                    }
                    $arr_img = $this->handle_picture_v3($val_program['ContentID']);
                    if (!empty($arr_img) && is_array($arr_img))
                    {
                        $val_program['key_val_list'] = array_merge($val_program['key_val_list'], $arr_img);
                    }

                    $add_index = array(
                        'base_info' => array(
                            'nns_name' => isset($val_program['key_val_list']['Name']) ? $val_program['key_val_list']['Name'] : '',
                            'nns_index' => $val_program['key_val_list']['Sequence'],
                            'nns_time_len' => '',
                            'nns_summary' => isset($val_program['key_val_list']['Description']) ? $val_program['key_val_list']['Description'] : '',
                            'nns_image' => isset($val_program['key_val_list']['Picture']) ? $val_program['key_val_list']['Picture'] : '',
                            'nns_play_count' => 0,
                            'nns_score_total' => 0,
                            'nns_score_count' => 0,
                            'nns_video_import_id' => $res['data_info'][0]['nns_asset_import_id'],
                            'nns_import_id' => $val_program['ContentID'],
                            'nns_import_source' => isset($val_program['nns_import_source']) ? $val_program['nns_import_source'] : evn::get("project"),
                            'nns_director' => isset($val_program['key_val_list']['WriterDisplay']) ? $val_program['key_val_list']['WriterDisplay'] : '',
                            'nns_actor' => isset($val_program['key_val_list']['ActorDisplay']) ? $val_program['key_val_list']['ActorDisplay'] : '',
                            'nns_release_time' => (isset($val_program['key_val_list']['ReleaseYear']) && strlen($val_program['key_val_list']['ReleaseYear']) > 0) ? date("Y-m-d", strtotime($val_program['key_val_list']['ReleaseYear'])) : date("Y-m-d"),
                            'nns_update_time' => '',
                            'nns_watch_focus' => '',
                            'nns_cp_id' => '',
                            'nns_conf_info' => '',
                            'nns_ext_url' => '',
                        ),
                        'ex_info' => array(
                            'isintact' => '',
                            'subordinate_name' => '',
                            'initials' => '',
                            'publisher' => '',
                            'first_spell' => '',
                            'caption_language' => '',
                            'language' => isset($val_program['key_val_list']['Language']) ? $val_program['key_val_list']['Language'] : '',
                            'region' => '',
                            'adaptor' => '',
                            'sreach_key' => '',
                            'event_tag' => '',
                            'year' => '',
                            'sort_name' => '',
                        ),
                    );

                    //分集注入
                    $re = $this->index_action('add', $add_index);
                    if ($re['ret'] != 0)
                    {
                        $re['ret'] = 1;
                        return $re;
                    }
                }
            }
        }
        //点播片源操作--云南广电
        if (!empty($arr_movie))
        {
            foreach ($arr_movie as $val_movie)
            {
                //片源删除
                if (strtoupper($val_movie['Action']) == 'DELETE')
                {

                    $del_movie = array(
                        'base_info' => array(
                            'nns_content_id' => isset($val_movie['assetid']) ? $val_movie['assetid']: '',
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_import_source' => isset($val_movie['nns_import_source']) ? $val_movie['nns_import_source'] : evn::get("project"),
                        ),
                    );
                    $re = $this->media_action('delete', $del_movie);
                    if ($re['ret'] != 0)
                    {
                        $re['ret'] = 1;
                        return $re;
                    }
                } //片源修改
                else
                {
                    $media_import_id = isset($val_movie['media_import_id']) ? $val_movie['media_import_id'] : '';
                    $video_import_id = $index_import_id = isset($val_movie['assetid']) ? $val_movie['assetid']: '';

                    $index_info = nl_asset_incidence_relation::query_asset_relation(m_config::get_dc(), 'index', $index_import_id, $message['nns_cp_id'], 1);
                    if (!is_array($index_info['data_info']))
                    {
                        $video_info = nl_asset_incidence_relation::query_asset_relation(m_config::get_dc(), 'video', $video_import_id, $message['nns_cp_id'], 0);
                        //先模拟注入主媒资
                        if (!is_array($video_info['data_info']) || empty($video_info['data_info']))
                        {
                            $view_type = "0";
                            $asset_category = '电影';
                            $do_category = array(
                                'base_info'=>array(
                                    'nns_name'=>$asset_category,   //栏目名称支持多层目录，多层目录的时候 /分割
                                    'nns_import_category_id'=>'', //栏目名称支持多层目录，多层目录的时候 /分割
                                    'nns_cp_id'=>$message['nns_cp_id'],
                                    'nns_import_parent_category_id'=>'',
                                    'nns_video_type'=>'0',  //媒资类型  0 点播 | 1 直播
                                ),
                            );
                            $result = $this->category_action('add', $do_category);
                            if($result['ret'] !=0)
                            {
                                return $result;
                            }
                            if(!isset($result['data_info']['base_info']['nns_id']) || strlen($result['data_info']['base_info']['nns_id']) <1)
                            {
                                return m_config::return_data(1,'创建栏目失败');
                            }
                            $nns_category_id = $result['data_info']['base_info']['nns_id'];

                            #TODO
                            $val_movie['volumename'] = $video_import_id;
                             
                            $pinyin = m_pinyin::get_pinyin_letter($val_movie['volumename']);
                            $add_series = array(
                                'base_info' => array(
                                    'nns_name' => isset($val_movie['volumename']) ? $val_movie['volumename'] : '',
                                    'nns_view_type' => $view_type,
                                    'nns_org_type' => '0',
                                    'nns_tag' => '26,',
                                    'nns_director' => '',
                                    'nns_actor' => '',
                                    'nns_show_time' => date("Y-m-d"),
                                    'nns_time_len' => 0,
                                    'nns_all_index' => 1,
                                    'nns_new_index' => 1,
                                    'nns_area' => '',
                                    'nns_image0' => '',
                                    'nns_image1' => '',
                                    'nns_image2' => '',
                                    'nns_image3' => '',
                                    'nns_image4' => '',
                                    'nns_image5' => '',
                                    'nns_summary' => '',
                                    'nns_remark' => '',
                                    'nns_category_id' => $nns_category_id,
                                    'nns_play_count' => '0',
                                    'nns_score_total' => '0',
                                    'nns_score_count' => '0',
                                    'nns_point' => '0',
                                    'nns_copyright_date' => date("Y"),
                                    'nns_asset_import_id' => $video_import_id,
                                    'nns_pinyin' => $pinyin,
                                    'nns_pinyin_length' => strlen($pinyin),
                                    'nns_alias_name' => '',
                                    'nns_eng_name' => '',
                                    'nns_language' => '国语',
                                    'nns_text_lang' => '中文',
                                    'nns_producer' => isset($val_movie['providerid']) ? $val_movie['providerid'] : '',
                                    'nns_screenwriter' => '',
                                    'nns_play_role' => '',
                                    'nns_copyright_range' => isset($val_movie['providerid']) ? $val_movie['providerid'] : '',
                                    'nns_vod_part' => '',
                                    'nns_keyword' => '',
                                    'nns_import_source' => isset($val_movie['nns_import_source']) ? $val_movie['nns_import_source'] : evn::get("project"),
                                    'nns_kind' => '',
                                    'nns_copyright' => isset($val_movie['providerid']) ? $val_movie['providerid'] : '',
                                    'nns_clarity' => '',
                                    'nns_image_v' => '',
                                    'nns_image_s' => '',
                                    'nns_image_h' => '',
                                    'nns_cp_id' => $message['nns_cp_id'],
                                    'nns_conf_info' => '',
                                    'nns_ext_url' => '',
                                ), //基本信息（存储于nns_vod表中）
                                'ex_info' => array(
                                    'svc_item_id' => '',
                                    'month_clicks' => '',
                                    'week_clicks' => '',
                                    'base_id' => '',
                                    'asset_path' => '',
                                    'ex_tag' => '',
                                    'full_spell' => '',
                                    'awards' => '',
                                    'year' => '',
                                    'play_time' => '',
                                    'channel' => '',
                                    'first_spell' => '',
                                ), //扩展信息（存储于nns_vod_ex表中）
                            );
                            //字段待修改4-14
                            $result = $this->vod_action('add', $add_series);
                            if ($result['ret'] != 0)
                            {
                                $result['ret'] = 1;
                                return $result;
                            }
                        }
                        $add_index = array(
                            'base_info' => array(
                                'nns_name' => isset($val_movie['volumename']) ? $val_movie['volumename'] . " 第1集" : '',
                                'nns_index' => 0,
                                'nns_time_len' => '',
                                'nns_summary' => '',
                                'nns_image' => '',
                                'nns_play_count' => 0,
                                'nns_score_total' => 0,
                                'nns_score_count' => 0,
                                'nns_cp_id' => $message['nns_cp_id'],
                                'nns_import_source' => isset($val_movie['nns_import_source']) ? $val_movie['nns_import_source'] : evn::get("project"),
                                'nns_video_import_id' => $video_import_id,
                                'nns_import_id' => $video_import_id,
                                'nns_director' => '',
                                'nns_actor' => '',
                                'nns_release_time' => date("Y-m-d"),
                                'nns_update_time' => date("Y-m-d H:i:s"),
                                'nns_watch_focus' => '',
                                'nns_conf_info' => '',
                                'nns_ext_url' => '',
                            ),
                            'ex_info' => array(
                                'isintact' => '',
                                'subordinate_name' => '',
                                'initials' => '',
                                'publisher' => '',
                                'first_spell' => '',
                                'caption_language' => '',
                                'language' => '',
                                'region' => '',
                                'adaptor' => '',
                                'sreach_key' => '',
                                'event_tag' => '',
                                'year' => '',
                                'sort_name' => '',
                            ),
                        );
                        //模拟分集注入
                        $re = $this->index_action('add', $add_index);
                        if ($re['ret'] != 0)
                        {
                            $re['ret'] = 1;
                            return $re;
                        }
                    }
                    $str_password = $str_username = '';
                    if(isset($val_movie['input']['username']) && strlen($val_movie['input']['username'])>0 && isset($val_movie['input']['password']) && strlen($val_movie['input']['password'])>0)
                    {
                        $val_movie['input']['sourceurl'] = $this->make_new_url($val_movie['input']['sourceurl'], $val_movie['input']['username'], $val_movie['input']['password']);
                    }
                    //片源参数
                    $add_media = array(
                        'base_info' => array(
                            'nns_name' => isset($val_movie['volumename']) ? $val_movie['volumename'] : '',
                            'nns_type' => 1,
                            'nns_url' => isset($val_movie['input']['sourceurl']) ? $val_movie['input']['sourceurl'] : '',
                            'nns_tag' => '26,',
                            'nns_mode' => '',
                            'nns_kbps' => isset($val_movie['input']['transferbitrate']) ? $val_movie['input']['transferbitrate'] : '8081',
                            'nns_content_id ' => $media_import_id,
                            'nns_content_state' => 0,
                            'nns_filetype' => 'ts',
                            'nns_play_count' => '0',
                            'nns_score_total' => '0',
                            'nns_score_count' => '0',
                            'nns_video_import_id' => $video_import_id,
                            'nns_index_import_id' => $index_import_id,
                            'nns_import_id' => $media_import_id,
                            'nns_import_source' => isset($val_movie['nns_import_source']) ? $val_movie['nns_import_source'] : evn::get("project"),
                            'nns_dimensions' => '2D',
                            'nns_ext_url' => json_encode($val_movie, JSON_UNESCAPED_UNICODE),
                            'nns_file_size' => 0,
                            'nns_file_time_len' => 0,
                            'nns_file_frame_rate' => 0,
                            'nns_file_resolution' => 0,
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_ext_info' => '',
                            'nns_drm_enabled' => 0,
                            'nns_drm_encrypt_solution' => '',
                            'nns_drm_ext_info' => '',
                            'nns_domain' => 0,
                            'nns_media_type' => 1,
                            'nns_original_live_id' => '',
                            'nns_start_time' => '',
                            'nns_media_service' => 'HTTP',
                            'nns_conf_info' => '',
                            'nns_encode_flag' => 0,
                            'nns_live_to_media' => '',
                            'nns_media_service_type' => '',
                        ),
                        'ex_info' => array(
                            'file_hash' => '',
                            'file_width' => '',
                            'file_height' => '',
                            'file_scale' => '',
                            'file_coding' => '',
                        ),
                    );

                    $re = $this->media_action('add', $add_media);
                    if ($re['ret'] != 0)
                    {
                        $re['ret'] = 1;
                        return $re;
                    }
                }
            }
        }

        //直播频道操作  直播频道的增删改和主媒资的相同
        if (!empty($arr_channel))
        {
            //循环频道，对每个频道进行处理
            foreach ($arr_channel as $val_channel)
            {
                //频道删除操作
                if (strtoupper($val_channel['Action']) == 'DELETE')
                {
                    $del_channel = array(
                        'base_info' => array(
                            'nns_import_id' => $val_channel['ContentID'],
                            'nns_import_source' => $val_channel['nns_import_source'],
                            'nns_cp_id' => $message['nns_cp_id'],
                        ),
                    );
                    $result = $this->live_action('delete', $del_channel);
                } //直播频道更新或者注入
                else
                {
                    //默认一级分类为
                    $view_type = 1;
                    if ($message['nns_cp_id'] == 'JSCN')
                    {
                        $val_channel['key_val_list']['CategoryName'] = (isset($val_channel['key_val_list']['PgmCategory']) && strlen($val_channel['key_val_list']['PgmCategory']) > 0) ? $val_channel['key_val_list']['PgmCategory'] : '卫士频道';
                    }
                    $asset_category = (isset($val_channel['key_val_list']['CategoryName']) && strlen($val_channel['key_val_list']['CategoryName']) > 0) ? $val_channel['key_val_list']['CategoryName'] : '卫士频道';
                    $category_import_id = (isset($val_channel['key_val_list']['CategoryID']) && strlen($val_channel['key_val_list']['CategoryID']) > 0) ? $val_channel['key_val_list']['CategoryID'] : '';

                    $do_category = array(
                        'base_info' => array(
                            'nns_name' => $asset_category,   //栏目名称支持多层目录，多层目录的时候 /分割
                            'nns_import_category_id' => $category_import_id, //栏目名称支持多层目录，多层目录的时候 /分割
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_import_parent_category_id' => '',
                            'nns_video_type' => '1',  //媒资类型  0 点播 | 1 直播
                        ),
                    );
                    $result = $this->category_action('add', $do_category);
                    if ($result['ret'] != 0)
                    {
                        return $result;
                    }
                    if (!isset($result['data_info']['base_info']['nns_id']) || strlen($result['data_info']['base_info']['nns_id']) < 1)
                    {
                        return m_config::return_data(1, '创建栏目失败');
                    }
                    $nns_category_id = $result['data_info']['base_info']['nns_id'];
                    //foreach ($this->category_map as $index => $v)
                    //{
                    //    if (stripos($v, $asset_category) !== false)
                    //    {
                    //        $view_type = $index;
                    //        break;
                    //    }
                    //}
                    //if($view_type == 1 && isset($val_channel['key_val_list']['VolumnCount']) && $val_channel['key_val_list']['VolumnCount'] >1)
                    //{
                    //    $view_type = 1;
                    //}
                    #TODO
                    $arr_img = $this->handle_picture_v3($val_channel['ContentID']);
                    if (!empty($arr_img) && is_array($arr_img))
                    {
                        $val_series['key_val_list'] = array_merge($val_channel['key_val_list'], $arr_img);
                    }
                    $pinyin = m_pinyin::get_pinyin_letter($val_channel['key_val_list']['Name']);

                    $ext_info = array();
                    //扩展表存放的扩展字段--频道号
                    if (!isset($val_channel['key_val_list']['ChannelNumber']) || strlen($val_channel['key_val_list']['ChannelNumber']))
                    {
                        $ext_info[] = array(
                            'nns_key' => 'live_pindao_order',
                            'nns_value' => $val_channel['key_val_list']['ChannelNumber'],
                            'nns_cp_id' => $message['nns_cp_id'],
                        );
                    }
                    $add_channel = array(
                        'base_info' => array(
                            'nns_name' => isset($val_channel['key_val_list']['Name']) ? $val_channel['key_val_list']['Name'] : '',
                            'nns_view_type' => $view_type,
                            'nns_org_type' => '0',
                            'nns_tag' => '26,',
                            'nns_director' => isset($val_channel['key_val_list']['WriterDisplay']) ? $val_channel['key_val_list']['WriterDisplay'] : '',
                            'nns_actor' => isset($val_channel['key_val_list']['ActorDisplay']) ? $val_channel['key_val_list']['ActorDisplay'] : '',
                            'nns_show_time' => (isset($val_channel['key_val_list']['ReleaseYear']) && strlen($val_channel['key_val_list']['ReleaseYear']) > 0) ? date("Y", strtotime($val_channel['key_val_list']['ReleaseYear'])) : date("Y"),
                            'nns_view_len' => isset($val_channel['key_val_list']['Duration']) ? $val_channel['key_val_list']['Duration'] : 0,
                            'nns_all_index' => isset($val_channel['key_val_list']['VolumnCount']) ? $val_channel['key_val_list']['VolumnCount'] : 1,
                            'nns_new_index' => isset($val_channel['key_val_list']['NewCount']) ? $val_channel['key_val_list']['NewCount'] : 0,
                            'nns_area' => isset($val_channel['key_val_list']['Country']) ? $val_channel['key_val_list']['Country'] : '',
                            'nns_image0' => isset($val_channel['key_val_list']['bigpic']) ? $val_channel['key_val_list']['bigpic'] : '',
                            'nns_image1' => isset($val_channel['key_val_list']['middlepic']) ? $val_channel['key_val_list']['middlepic'] : '',
                            'nns_image2' => isset($val_channel['key_val_list']['smallpic']) ? $val_channel['key_val_list']['smallpic'] : '',
                            'nns_image3' => '',
                            'nns_image4' => '',
                            'nns_image5' => '',
                            'nns_summary' => isset($val_channel['key_val_list']['Description']) ? $val_channel['key_val_list']['Description'] : '',
                            'nns_remark' => isset($val_channel['key_val_list']['Kind']) ? $val_channel['key_val_list']['Kind'] : '',
                            'nns_category_id' => $nns_category_id,
                            'nns_play_count' => '0',
                            'nns_score' => '0',
                            'nns_score_count' => '0',
                            'nns_point' => isset($val_channel['key_val_list']['ViewPoint']) ? $val_channel['key_val_list']['ViewPoint'] : '0',
                            'nns_copyright_date' => (isset($val_channel['key_val_list']['ReleaseYear']) && strlen($val_channel['key_val_list']['ReleaseYear']) > 0) ? date("Y", strtotime($val_channel['key_val_list']['ReleaseYear'])) : date("Y"),
                            'nns_import_id' => $val_channel['ContentID'],
                            'nns_pinyin' => (isset($val_channel['key_val_list']['Spell']) && strlen($val_channel['key_val_list']['Spell']) > 0) ? $val_channel['key_val_list']['Spell'] : $pinyin,
                            'nns_pinyin_length' => (isset($val_channel['key_val_list']['Spell']) && strlen($val_channel['key_val_list']['Spell']) > 0) ? strlen($val_channel['key_val_list']['Spell']) : strlen($pinyin),
                            'nns_alias_name' => isset($val_channel['key_val_list']['CallSign']) ? $val_channel['key_val_list']['CallSign'] : '',
                            'nns_eng_name' => isset($val_channel['key_val_list']['EnglishName']) ? $val_channel['key_val_list']['EnglishName'] : '',
                            'nns_language' => isset($val_channel['key_val_list']['Language']) ? $val_channel['key_val_list']['Language'] : '',
                            'nns_producer' => isset($val_channel['key_val_list']['ContentProvider']) ? $val_channel['key_val_list']['ContentProvider'] : '',
                            'nns_import_source' => isset($val_channel['nns_import_source']) ? $val_channel['nns_import_source'] : evn::get("project"),
                            'nns_image_v' => isset($val_channel['key_val_list']['verticality_img']) ? $val_channel['key_val_list']['verticality_img'] : '',
                            'nns_image_s' => isset($val_channel['key_val_list']['square_img']) ? $val_channel['key_val_list']['square_img'] : '',
                            'nns_image_h' => isset($val_channel['key_val_list']['horizontal_img']) ? $val_channel['key_val_list']['horizontal_img'] : '',
                            'nns_image_t' => '',
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_ext_info' => '',
                        ), //基本信息（存储于nns_live表中）
                        'ex_info' => $ext_info,//扩展信息（存储于nns_live_ex表中）
                    );
                    //字段待修改4-14
                    $result = $this->live_action('add', $add_channel);
                }
                if ($result['ret'] != 0)
                {
                    break;
                }
            }
        }

        //直播频道片源--云南广电
        if (!empty($arr_live_media))
        {
            foreach ($arr_live_media as $val_live_media)
            {
                //删除
                if (strtoupper($val_live_media['Action']) == 'DELETE')
                {
                    $del_live_media = array(
                        'base_info' => array(
                            'nns_content_id' => isset($val_live_media['assetid']) ? $val_live_media['assetid']: '',
                            'nns_import_source' => isset($val_live_media['nns_import_source']) ? $val_live_media['nns_import_source'] : evn::get("project"),
                            'nns_cp_id' => $message['nns_cp_id'],
                        ),
                    );
                    $re = $this->live_media_action('delete', $del_live_media);
                    if ($re['ret'] != 0)
                    {
                        return $re;
                    }
                } //注入和修改
                else
                {
                    $live_media_import_id = isset($val_live_media['media_import_id']) ? $val_live_media['media_import_id'] : '';
                    $live_import_id = $live_index_import_id  = isset($val_live_media['assetid']) ? $val_live_media['assetid']: '';
                    $res_channel = nl_live::query_by_condition(m_config::get_dc(), array('nns_import_id' => $live_import_id));
                    if (!is_array($res_channel['data_info']) || empty($res_channel['data_info']))
                    {
                        //模拟直播注入
                        //默认一级分类为
                        $view_type = 1;
                        $asset_category = '卫视频道';
                        $do_category = array(
                            'base_info' => array(
                                'nns_name' => $asset_category,   //栏目名称支持多层目录，多层目录的时候 /分割
                                'nns_import_category_id' => '', //栏目名称支持多层目录，多层目录的时候 /分割
                                'nns_cp_id' => $message['nns_cp_id'],
                                'nns_import_parent_category_id' => '',
                                'nns_video_type' => '1',  //媒资类型  0 点播 | 1 直播
                            ),
                        );
                        $result = $this->category_action('add', $do_category);
                        if ($result['ret'] != 0)
                        {
                            return $result;
                        }
                        if (!isset($result['data_info']['base_info']['nns_id']) || strlen($result['data_info']['base_info']['nns_id']) < 1)
                        {
                            return m_config::return_data(1, '创建栏目失败');
                        }
                        $nns_category_id = $result['data_info']['base_info']['nns_id'];

                        #TODO
                        $pinyin = m_pinyin::get_pinyin_letter($val_live_media['volumeName']);

                        $add_channel = array(
                            'base_info' => array(
                                'nns_name' => isset($val_live_media['volumename']) ? $val_live_media['volumename'] : '',
                                'nns_view_type' => $view_type,
                                'nns_org_type' => '0',
                                'nns_tag' => '26,',
                                'nns_director' => '',
                                'nns_actor' => '',
                                'nns_show_time' => date("Y"),
                                'nns_time_len' => 0,
                                'nns_all_index' => 1,
                                'nns_new_index' => 0,
                                'nns_area' => '',
                                'nns_image0' => '',
                                'nns_image1' => '',
                                'nns_image2' => '',
                                'nns_image3' => '',
                                'nns_image4' => '',
                                'nns_image5' => '',
                                'nns_summary' => '',
                                'nns_remark' => '',
                                'nns_category_id' => $nns_category_id,
                                'nns_play_count' => '0',
                                'nns_score' => '0',
                                'nns_score_count' => '0',
                                'nns_point' => '0',
                                'nns_copyright_date' => date("Y"),
                                'nns_import_id' => $live_import_id,
                                'nns_pinyin' => $pinyin,
                                'nns_pinyin_length' => strlen($pinyin),
                                'nns_alias_name' => '',
                                'nns_eng_name' => '',
                                'nns_language' => '国语',
                                'nns_producer' => isset($val_live_media['providerid']) ? $val_live_media['providerid'] : '',
                                'nns_import_source' => isset($val_live_media['nns_import_source']) ? $val_live_media['nns_import_source'] : evn::get("project"),
                                'nns_image_v' => '',
                                'nns_image_s' => '',
                                'nns_image_h' => '',
                                'nns_image_t' => '',
                                'nns_cp_id' => $message['nns_cp_id'],
                                'nns_ext_info' => '',
                            ), //基本信息（存储于nns_live表中）
                            'ex_info' => array(
                                'live_pindao_order' => $live_import_id,
                                'live_type' => 'LIVE',
                                'dvb' => '',
                                'back_record_day_type' => '',
                                'back_record_day' => 0,
                                'pid' => $live_import_id,
                                'ts_limit_min' => 0,
                                'ts_limit_max' => 0,
                                'ts_default_pos' => 0,
                            ),//扩展信息（存储于nns_live_ex表中）
                        );
                        //字段待修改4-14
                        $result = $this->live_action('add', $add_channel);
                        
                        if ($result['ret'] != 0)
                        {
                            return $result;
                        }
                    }
                    // 仿照分集
                    $str_index_id = np_guid_rand();
                    $add_live_index = array(
                        'base_info' => array(
                            'nns_name' => isset($val_live_media['volumename']) ? $val_live_media['volumename'] : '',
                            'nns_index' => 0,
                            'nns_time_len' => 3600,
                            'nns_summary' => '',
                            'nns_image' => '',
                            'nns_play_count' => 0,
                            'nns_score' => 0,
                            'nns_score_count' => 0,
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_import_id' => $live_index_import_id,
                            'nns_live_import_id' => $live_import_id,
                            'nns_import_source' => isset($val_live_media['nns_import_source']) ? $val_live_media['nns_import_source'] : evn::get("project"),
                        ),
                        'hide_info' => array(
                            'nns_id' => $str_index_id,
                            'nns_live_id' => $res_channel['data_info'][0]['nns_id'],
                        )
                    );
                    $index_query = array(
                        'nns_cp_id' => $message['nns_cp_id'],
                        'nns_import_id' => $live_index_import_id,
                        'nns_import_source'=> evn::get("project"),
                    );
                    $result_exist = nl_live_index::query_by_condition(m_config::get_dc(), $index_query);
                    if (!is_array($result_exist['data_info']) || empty($result_exist['data_info']) || !isset($result_exist['data_info'][0]) || !is_array($result_exist['data_info'][0]) || empty($result_exist['data_info'][0]))
                    {
                        $re = $this->live_index_action('add', $add_live_index);

                        if ($re['ret'] != 0)
                        {
                            return $re;
                        }
                        
                    }
                    else
                    {
                        $str_index_id = $result_exist['data_info'][0]['nns_id'];
                    }

                    //直播片源参数
                    $add_live_media = array(
                        'base_info' => array(
                            'nns_name' => isset($val_live_media['volumename']) ? $val_live_media['volumename'] : '',
                            'nns_type' => 1,
                            'nns_url' => isset($val_live_media['input']['sourceurl']) ? $val_live_media['input']['sourceurl'] : '',
                            'nns_tag' => '26,',
                            'nns_mode' => '',
                            'nns_kbps' => isset($val_live_media['input']['transferbitrate']) ? $val_live_media['input']['transferbitrate'] : '',
                            'nns_content_id' => $live_media_import_id,
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_import_source' => isset($val_live_media['nns_import_source']) ? $val_live_media['nns_import_source'] : evn::get("project"),
                            'nns_content_state' => 0,
                            'nns_filetype' => 'ts',
                            'nns_play_count' => '0',
                            'nns_score_total' => '0',
                            'nns_score_count' => '0',
                            'nns_media_cps' => 'LIVE,TSTV,PLAYBACK',
                            'nns_cast_type' => 0,
                            'nns_message_id' => $message['nns_id'],
                            'nns_timeshift_status' => 1,
                            'nns_timeshift_delay' => 168,
                            'nns_storage_status' => 1,
                            'nns_storage_delay' => 168,
                            'nns_drm_enabled' => 0,
                            'nns_drm_encrypt_solution' => '',
                            'nns_drm_ext_info' => '',
                            'nns_domain' => '',
                            'nns_media_service' => 'MSP',
                            'nns_ext_url' => json_encode($val_live_media, JSON_UNESCAPED_UNICODE),
                            'nns_live_import_id' => $live_import_id,
                            'nns_live_index_import_id' => $live_index_import_id,
                        ), //基本信息（存储于nns_live表中）
                        'hide_info' => array(
                            'nns_id' => np_guid_rand(),
                            'nns_state' => 1,
                            'nns_deleted' => 0,
                            'nns_check' => '',
                            'nns_live_id' => $str_index_id,
                        ),
                    );

                    $re = $this->live_media_action('add', $add_live_media);
                    if ($re['ret'] != 0)
                    {
                        $re['ret'] = 1;
                        return $re;
                    }
                }
            }
        }

        //节目单注入
        if (!empty($arr_playbill))
        {
            //循环节目单，对每个节目单进行处理
            foreach ($arr_playbill as $val_playbill)
            {
                //频道删除操作
                if (strtoupper($val_playbill['Action']) == 'DELETE')
                {
                    $del_playbill = array(
                        'base_info' => array(
                            'nns_playbill_import_id' => $val_playbill['ContentID'],
                            'nns_import_source' => isset($val_playbill['nns_import_source']) ? $val_playbill['nns_import_source'] : evn::get("project"),
                            'nns_cp_id' => $message['nns_cp_id'],
                        ),
                    );
                    $result = $this->playbill_action('delete', $del_playbill);
                } //节目单更新或者注入
                else
                {
                    $val_playbill['key_val_list']['Name'] = $val_playbill['assetid'];
                    $pinyin = m_pinyin::get_pinyin_letter($val_playbill['key_val_list']['Name']);

                    // $val_playbill['captureStart'] = str_replace('T', ' ', $val_playbill['captureStart']);
                    // $val_playbill['captureStart'] = str_replace('Z', '', $val_playbill['captureStart']);
                    //开始时间
                    $begin_time = date('Y-m-d H:i:s', strtotime($val_playbill['capturestart']));
                    $end_time =  date('Y-m-d H:i:s', strtotime($val_playbill['captureend']));
                    
                    $time_len = strtotime($end_time)-strtotime($begin_time);
                    $time_len = abs($time_len);
                    $live_id = md5(strtolower($val_playbill['sourceurl']));
                    $res_channel = nl_live::query_by_condition(m_config::get_dc(), array('nns_import_id' => $live_id,'nns_cp_id'=>$message['nns_cp_id'],'nns_import_source'=>isset($val_playbill['nns_import_source']) ? $val_playbill['nns_import_source'] : evn::get("project")));
                    if (!is_array($res_channel['data_info']) || empty($res_channel['data_info']))
                    {
                        //模拟直播注入
                        //默认一级分类为
                        $view_type = 1;
                        $asset_category = '卫视频道';
                        $do_category = array(
                            'base_info' => array(
                                'nns_name' => $asset_category,   //栏目名称支持多层目录，多层目录的时候 /分割
                                'nns_import_category_id' => '', //栏目名称支持多层目录，多层目录的时候 /分割
                                'nns_cp_id' => $message['nns_cp_id'],
                                'nns_import_parent_category_id' => '',
                                'nns_video_type' => '1',  //媒资类型  0 点播 | 1 直播
                            ),
                        );
                        $result = $this->category_action('add', $do_category);
                        if ($result['ret'] != 0)
                        {
                            return $result;
                        }
                        if (!isset($result['data_info']['base_info']['nns_id']) || strlen($result['data_info']['base_info']['nns_id']) < 1)
                        {
                            return m_config::return_data(1, '创建栏目失败');
                        }
                        $nns_category_id = $result['data_info']['base_info']['nns_id'];
                    
                        #TODO
                        $pinyin = m_pinyin::get_pinyin_letter($live_id);
                        $live_pindao_order = rand(1,999);
                        $add_channel = array(
                            'base_info' => array(
                                'nns_name' => $live_id,
                                'nns_view_type' => $view_type,
                                'nns_org_type' => '0',
                                'nns_tag' => '26,',
                                'nns_director' => '',
                                'nns_actor' => '',
                                'nns_show_time' => date("Y"),
                                'nns_view_len' => 0,
                                'nns_all_index' => 1,
                                'nns_new_index' => 0,
                                'nns_area' => '',
                                'nns_image0' => '',
                                'nns_image1' => '',
                                'nns_image2' => '',
                                'nns_image3' => '',
                                'nns_image4' => '',
                                'nns_image5' => '',
                                'nns_summary' => '',
                                'nns_remark' => '',
                                'nns_category_id' => $nns_category_id,
                                'nns_play_count' => '0',
                                'nns_score' => '0',
                                'nns_score_count' => '0',
                                'nns_point' => '0',
                                'nns_copyright_date' => date("Y"),
                                'nns_import_id' => $live_id,
                                'nns_pinyin' => $pinyin,
                                'nns_pinyin_length' => strlen($pinyin),
                                'nns_alias_name' => '',
                                'nns_eng_name' => '',
                                'nns_language' => '国语',
                                'nns_producer' => '',
                                'nns_import_source' => isset($val_playbill['nns_import_source']) ? $val_playbill['nns_import_source'] : evn::get("project"),
                                'nns_image_v' => '',
                                'nns_image_s' => '',
                                'nns_image_h' => '',
                                'nns_image_t' => '',
                                'nns_cp_id' => $message['nns_cp_id'],
                                'nns_ext_info' => '',
                            ), //基本信息（存储于nns_live表中）
                            'ex_info' => array(
                                'live_pindao_order' => $live_pindao_order,
                                'live_type' => 'LIVE,TSTV,PLAYBACK',
                                'dvb' => '',
                                'back_record_day_type' => '',
                                'back_record_day' => 0,
                                'pid' => $live_pindao_order,
                                'ts_limit_min' => 0,
                                'ts_limit_max' => 0,
                                'ts_default_pos' => 0,
                            ),//扩展信息（存储于nns_live_ex表中）
                        );
                        //字段待修改4-14
                        $result = $this->live_action('add', $add_channel);
                    
                        if ($result['ret'] != 0)
                        {
                            return $result;
                        }
                    }

                    $add_live_index = array(
                        'base_info' => array(
                            'nns_name' => $live_id,
                            'nns_index' => 0,
                            'nns_time_len' => 3600,
                            'nns_summary' => '',
                            'nns_image' => '',
                            'nns_play_count' => 0,
                            'nns_score' => 0,
                            'nns_score_count' => 0,
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_import_id' => $live_id,
                            'nns_live_import_id' => $live_id,
                            'nns_import_source' => isset($val_playbill['nns_import_source']) ? $val_playbill['nns_import_source'] : evn::get("project"),
                        ),
                    );
                    $index_query = array(
                        'nns_cp_id' => $message['nns_cp_id'],
                        'nns_import_id' => $live_id,
                        'nns_import_source'=> evn::get("project"),
                    );
                    $result_exist = nl_live_index::query_by_condition(m_config::get_dc(), $index_query);
                    if (!is_array($result_exist['data_info']) || empty($result_exist['data_info']) || !isset($result_exist['data_info'][0]) || !is_array($result_exist['data_info'][0]) || empty($result_exist['data_info'][0]))
                    {
                        $re = $this->live_index_action('add', $add_live_index);
                    
                        if ($re['ret'] != 0)
                        {
                            return $re;
                        }
                    
                    }
                    $live_media_query = array(
                        'nns_cp_id' => $message['nns_cp_id'],
                        'nns_content_id' => $live_id,
                        'nns_import_source'=> evn::get("project"),
                    );
                    $result_exist = nl_live_media::query_by_condition(m_config::get_dc(), $live_media_query);
                    if (!is_array($result_exist['data_info']) || empty($result_exist['data_info']) || !isset($result_exist['data_info'][0]) || !is_array($result_exist['data_info'][0]) || empty($result_exist['data_info'][0]))
                    {
                        //直播片源参数
                        $add_live_media = array(
                            'base_info' => array(
                                'nns_name' => $live_id,
                                'nns_type' => 1,
                                'nns_url' => isset($val_playbill['sourceurl']) ? $val_playbill['sourceurl'] : '',
                                'nns_tag' => '26,',
                                'nns_mode' => '',
                                'nns_kbps' => isset($val_playbill['transferbitrate']) ? $val_playbill['transferbitrate'] : '8081',
                                'nns_content_id' => $live_id,
                                'nns_cp_id' => $message['nns_cp_id'],
                                'nns_import_source' => isset($val_playbill['nns_import_source']) ? $val_playbill['nns_import_source'] : evn::get("project"),
                                'nns_content_state' => 0,
                                'nns_filetype' => 'ts',
                                'nns_play_count' => '0',
                                'nns_score_total' => '0',
                                'nns_score_count' => '0',
                                'nns_media_caps' => 'LIVE,TSTV,PLAYBACK',
                                'nns_cast_type' => 1,
                                'nns_message_id' => $message['nns_id'],
                                'nns_timeshift_status' => 1,
                                'nns_timeshift_delay' => 168,
                                'nns_storage_status' => 1,
                                'nns_storage_delay' => 168,
                                'nns_drm_enabled' => 0,
                                'nns_drm_encrypt_solution' => '',
                                'nns_drm_ext_info' => '',
                                'nns_domain' => '',
                                'nns_media_service' => 'MSP',
                                'nns_ext_url' => json_encode($val_playbill, JSON_UNESCAPED_UNICODE),
                                'nns_live_import_id' => $live_id,
                                'nns_live_index_import_id' => $live_id,
                            ), //基本信息（存储于nns_live表中）
                            'hide_info' => array(
                                'nns_state' => 1,
                                'nns_deleted' => 0,
                                'nns_check' => '',
                            ),
                        );
                        $re = $this->live_media_action('add', $add_live_media);
                        if ($re['ret'] != 0)
                        {
                            $re['ret'] = 1;
                            return $re;
                        }
                    }

                    
                    $add_playbill = array(
                        'base_info' => array(
                            'nns_name' => isset($val_playbill['key_val_list']['Name']) ? $val_playbill['key_val_list']['Name'] : '',
                            'nns_view_type' => 1,
                            'nns_begin_time' => $begin_time,
                            'nns_end_time' => $end_time,
                            'nns_time_len' => $time_len,
                            'nns_live_import_id' => $live_id,
                            'nns_summary' => isset($val_playbill['key_val_list']['Name']) ? $val_playbill['key_val_list']['Name'] : '',
                            'nns_image0' => '',
                            'nns_image1' => '',
                            'nns_image2' => '',
                            'nns_image3' => '',
                            'nns_image4' => '',
                            'nns_image5' => '',
                            'nns_pinyin' => $pinyin,
                            'nns_pinyin_length' => strlen($pinyin),
                            'nns_eng_name' => '',
                            'nns_keyword' => isset($val_playbill['key_val_list']['Name']) ? $val_playbill['key_val_list']['Name'] : '',
                            'nns_kind' => isset($val_playbill['key_val_list']['Name']) ? $val_playbill['key_val_list']['Name'] : '',
                            'nns_actor' => '',
                            'nns_director' => '',
                            'nns_live_media_import_id' => $live_id,
                            'nns_playbill_import_id' => $val_playbill['assetid'],
                            'nns_domain' => '',
                            'nns_hot_dgree' => 0,
                            'nns_cp_id' => $message['nns_cp_id'],
                            'nns_import_source' => isset($val_playbill['nns_import_source']) ? $val_playbill['nns_import_source'] : evn::get("project"),
                            'nns_ext_url' => '',
                            'nns_image_v' => '',
                            'nns_image_h' => '',
                            'nns_image_s' => '',
                        ), //基本信息（存储于nns_live表中）
                        'hide_info' => array(
                            'nns_state' => 0,
                            'nns_deleted' => 0,
                            'nns_check' => 1,
                        )
                    );
                    //字段待修改4-14
                    $result = $this->playbill_action('add', $add_playbill);
                    if ($result['ret'] != 0)
                    {
                        $result['ret'] = 1;
                        return $result;
                    }
                }
            }
        }


        if (!empty($arr_del_media))
        {
            foreach ($arr_del_media as $val_del_media)
            {
                $del_media = array(
                    'base_info' => array(
                        'nns_import_id' => isset($val_del_media['assetid']) ? $val_del_media['assetid'] : '',
                        'nns_import_source' => isset($val_live_media['nns_import_source']) ? $val_del_media['nns_import_source'] : evn::get("project"),
                        'nns_cp_id' => $message['nns_cp_id'],
                    ),
                );
                //先查询点播片源
                $vod_media_execute = $this->media_action('query', $del_media);
                
                if ($vod_media_execute['ret'] != 0)
                {
                    return $vod_media_execute;
                }
                if (!empty($vod_media_execute['data_info'][0]['base_info']) && isset($vod_media_execute['data_info'][0]['base_info']) && is_array($vod_media_execute['data_info'][0]['base_info']))
                {
                    $result = $this->media_action('delete', $del_media);
                    return $result;
                }
                unset($del_media['base_info']['nns_import_id']);

                //再查直播片源
                $live_media_execute = $this->live_media_action('query', $del_media);

                if ($live_media_execute['ret'] != 0)
                {
                    return $live_media_execute;
                }
                if (!empty($live_media_execute['data_info'][0]['base_info']) && isset($live_media_execute['data_info'][0]['base_info']) && is_array($live_media_execute['data_info'][0]['base_info']))
                {
                    $del_media['base_info']['nns_content_id'] = isset($val_del_media['assetid']) ? $val_del_media['assetid']: '';
                    $result = $this->live_media_action('delete', $del_media);
                    return $result;
                }

                return array('ret' => 0, 'OK.NO Content!');
            }
        }
	}
	
	/**
	 * 
	 * @param unknown $str_url
	 * @param string $str_user
	 * @param string $str_password
	 */
	private function make_new_url($str_url,$str_user,$str_password)
	{
	    $temp_server_url = parse_url($str_url);
        $str_url = '';
        if(isset($temp_server_url['scheme']) && strlen($temp_server_url['scheme'])>0)
        {
            $str_url.=$temp_server_url['scheme']."://";
        }
        if(strlen($str_user) >0 && strlen($str_password) >0)
        {
            $str_url.="{$str_user}:{$str_password}@";
        }
        if(isset($temp_server_url['host']) && strlen($temp_server_url['host'])>0)
        {
            $str_url.=$temp_server_url['host'];
        }
        if(isset($temp_server_url['port']) && strlen($temp_server_url['port'])>0)
        {
            $str_url.=":".$temp_server_url['port'];
        }
        if(isset($temp_server_url['path']) && strlen($temp_server_url['path'])>0)
        {
            $str_url.=$temp_server_url['path'];
        }
        if(isset($temp_server_url['query']) && strlen($temp_server_url['query'])>0)
        {
            $str_url.='?'.$temp_server_url['query'];
        }
        return $str_url;
	}

	private function handle_picture_v3($asset_id)
	{
	    $array_img = array();
	    $array_img['bigpic']='';
	    $array_img['middlepic']='';
	    $array_img['smallpic']='';
	    $array_img['horizontal_img']='';
	    $array_img['verticality_img']='';
	    $array_img['square_img']='';
	    if(empty($this->arr_picture) || !is_array($this->arr_picture))
	    {
	        return $array_img;
	    }
	    $array_map = $this->get_map_info_v2('Picture',$this->arr_map,$asset_id);
	    if(empty($array_map))
	    {
	        return $array_img;
	    }
	    $array_map_key = array_keys($array_map);
	    $temp_array = array();
	    foreach ($this->arr_picture as $obj_picture)
	    {
	        if(!in_array($obj_picture['ContentID'], $array_map_key))
	        {
	            continue;
	        }
	        //组成数据,key为图片type,值为url
	        if (!isset($obj_picture['key_val_list']['Type']))
	        {
	            foreach ($array_map as $k_id => $val)
	            {
	                if ($obj_picture['ContentID'] == $k_id)
	                {
	                    $obj_picture['key_val_list']['Type'] = $val['key_val_list']['Type'];
	                }
	            }
	        }
	        $temp_array[$obj_picture['key_val_list']['Type']] = (isset($obj_picture['key_val_list']['FileURL']) && strlen($obj_picture['key_val_list']['FileURL']) >0) ? trim($obj_picture['key_val_list']['FileURL']) : '';
	    }
	    $temp_array = array_filter($temp_array);
	    if(empty($temp_array))
	    {
	        return $array_img;
	    }
	    $arr_combine = $this->combine_picture($array_img, $temp_array);
	    return array_combine(array_keys($array_img),$arr_combine);
	}
	
	private function get_map_info_v2($p_type,$arr_map, $e_id)
	{
	    $temp_arr = array();
        foreach ($arr_map as $map)
        {
            if ($map['attributes']['ElementType'] == $p_type && $map['attributes']['ParentCode'] == $e_id)
            {
                $filter_map = $this->make_key_value_arr($map);
                $temp_arr[$map['attributes']['ElementCode']] = $filter_map;
            }
            if(isset($temp_arr[$map['attributes']['ElementCode']]))
            {
                continue;
            }
            if ($map['attributes']['ParentType'] == $p_type && $map['attributes']['ElementCode'] == $e_id)
            {
                $filter_map = $this->make_key_value_arr($map);
                $temp_arr[$map['attributes']['ParentCode']] = $filter_map;
            }
        }
	    return $temp_arr;
	}
	
	/**
	 * 获取xml的 attr属性和key value值
	 * @param unknown $xml_obj_arr
	 * @return Ambigous <NULL, unknown, string>
	 */
	private function make_key_value_arr($xml_obj_arr)
	{
	    $key_val_array = null;
	    if (isset($xml_obj_arr['attributes']) && is_array($xml_obj_arr['attributes']) && !empty($xml_obj_arr['attributes']))
	    {
	        foreach ($xml_obj_arr['attributes'] as $attr_key => $attr_val)
	        {
	            $key_val_array[$attr_key] = $attr_val;
	        }
	        if(!isset($key_val_array['ContentID']) || strlen($key_val_array['ContentID'])<1)
	        {
	            $key_val_array['ContentID'] = isset($xml_obj_arr['attributes']['ID']) ? $xml_obj_arr['attributes']['ID'] : '';
	        }
	        unset($xml_obj_arr['attributes']);
	    }
	    if (isset($xml_obj_arr['children']) && is_array($xml_obj_arr['children']) && !empty($xml_obj_arr['children']))
	    {
	        foreach ($xml_obj_arr['children'] as $key_list)
	        {
	            if (isset($key_list['attributes']['Name']) && strlen($key_list['attributes']['Name']) > 0)
	            {
	                $key_val_array['key_val_list'][$key_list['attributes']['Name']] = (isset($key_list['content']) && strlen($key_list['content']) > 0) ? trim($key_list['content']) : '';
	            }
	        }
	        unset($xml_obj_arr['children']);
	    }
	    return $key_val_array;
	}



    /**
     * 获取xml的 attr属性和key value值
     * @param unknown $xml_obj_arr
     * @return Ambigous <NULL, unknown, string>
     */
    private function make_key_value_arr_v2($xml_obj_arr)
    {
        $key_val_array = null;
        if (isset($xml_obj_arr['attributes']) && is_array($xml_obj_arr['attributes']) && !empty($xml_obj_arr['attributes']))
        {
            foreach ($xml_obj_arr['attributes'] as $attr_key => $attr_val)
            {
                $key_val_array[$attr_key] = $attr_val;
            }
            if(!isset($key_val_array['ContentID']) || strlen($key_val_array['ContentID'])<1)
            {
                $key_val_array['ContentID'] = isset($xml_obj_arr['attributes']['ID']) ? $xml_obj_arr['attributes']['ID'] : '';
            }
            unset($xml_obj_arr['attributes']);
        }
        if (isset($xml_obj_arr['children']) && is_array($xml_obj_arr['children']) && !empty($xml_obj_arr['children']))
        {
            foreach ($xml_obj_arr['children'] as $key=>$key_list)
            {
                if (isset($key_list['attributes']) && is_array($key_list['attributes']))
                {
                    $key_val_array['key_val_list'][$key_list['name']][] = $key_list['attributes'];
                }
            }
            unset($xml_obj_arr['children']);
        }
        return $key_val_array;
    }

	public function status($message_id)
	{
		
	}

	/**
	 * 
	 * @param unknown $message_id
	 * @param unknown $code 所有成功及成功 否则失败
	 * @param unknown $reason  
	 * @param unknown $data
	 */
	public function is_ok($message_id,$code,$reason,$data)
	{
	    
	}


    public function mu_array_change_key_case($array=null)
    {
        if(!is_array($array) || empty($array))
        {
            return $array;
        }
        $array = array_change_key_case($array);
        foreach ($array as $key=>&$val)
        {
            if(is_array($val) && !empty($val))
            {
                $array[$key] = $this->mu_array_change_key_case($val);
            }
        }
        return $array;
    }
}